package cn.ctodb.wxmp.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.wxmp.service.MessageTpService;
import cn.ctodb.wxmp.web.rest.util.HeaderUtil;
import cn.ctodb.wxmp.web.rest.util.PaginationUtil;
import cn.ctodb.wxmp.service.dto.MessageTpDTO;

import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing MessageTp.
 */
@RestController
@RequestMapping("/api")
public class MessageTpResource {

    private final Logger log = LoggerFactory.getLogger(MessageTpResource.class);
        
    @Inject
    private MessageTpService messageTpService;

    /**
     * POST  /message-tps : Create a new messageTp.
     *
     * @param messageTpDTO the messageTpDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new messageTpDTO, or with status 400 (Bad Request) if the messageTp has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/message-tps")
    @Timed
    public ResponseEntity<MessageTpDTO> createMessageTp(@Valid @RequestBody MessageTpDTO messageTpDTO) throws URISyntaxException {
        log.debug("REST request to save MessageTp : {}", messageTpDTO);
        if (messageTpDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("messageTp", "idexists", "A new messageTp cannot already have an ID")).body(null);
        }
        MessageTpDTO result = messageTpService.save(messageTpDTO);
        return ResponseEntity.created(new URI("/api/message-tps/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("messageTp", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /message-tps : Updates an existing messageTp.
     *
     * @param messageTpDTO the messageTpDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated messageTpDTO,
     * or with status 400 (Bad Request) if the messageTpDTO is not valid,
     * or with status 500 (Internal Server Error) if the messageTpDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/message-tps")
    @Timed
    public ResponseEntity<MessageTpDTO> updateMessageTp(@Valid @RequestBody MessageTpDTO messageTpDTO) throws URISyntaxException {
        log.debug("REST request to update MessageTp : {}", messageTpDTO);
        if (messageTpDTO.getId() == null) {
            return createMessageTp(messageTpDTO);
        }
        MessageTpDTO result = messageTpService.save(messageTpDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("messageTp", messageTpDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /message-tps : get all the messageTps.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of messageTps in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/message-tps")
    @Timed
    public ResponseEntity<List<MessageTpDTO>> getAllMessageTps(@ApiParam Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to get a page of MessageTps");
        Page<MessageTpDTO> page = messageTpService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/message-tps");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /message-tps/:id : get the "id" messageTp.
     *
     * @param id the id of the messageTpDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the messageTpDTO, or with status 404 (Not Found)
     */
    @GetMapping("/message-tps/{id}")
    @Timed
    public ResponseEntity<MessageTpDTO> getMessageTp(@PathVariable Long id) {
        log.debug("REST request to get MessageTp : {}", id);
        MessageTpDTO messageTpDTO = messageTpService.findOne(id);
        return Optional.ofNullable(messageTpDTO)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /message-tps/:id : delete the "id" messageTp.
     *
     * @param id the id of the messageTpDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/message-tps/{id}")
    @Timed
    public ResponseEntity<Void> deleteMessageTp(@PathVariable Long id) {
        log.debug("REST request to delete MessageTp : {}", id);
        messageTpService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("messageTp", id.toString())).build();
    }

    /**
     * SEARCH  /_search/message-tps?query=:query : search for the messageTp corresponding
     * to the query.
     *
     * @param query the query of the messageTp search 
     * @param pageable the pagination information
     * @return the result of the search
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/_search/message-tps")
    @Timed
    public ResponseEntity<List<MessageTpDTO>> searchMessageTps(@RequestParam String query, @ApiParam Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to search for a page of MessageTps for query {}", query);
        Page<MessageTpDTO> page = messageTpService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/message-tps");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }


}
